<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\bill;

use app\common\dao\BaseDao;
use app\common\model\bill\BillBasicModel;

class BillBasicDao extends BaseDao
{

    /**
     * @return BillBasicModel
     */
    protected function getModel(): string
    {
        return BillBasicModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['property_id']) && $where['property_id'] !== '', function ($query) use ($where) {
                $query->where('property_id', intval($where['property_id']));
            })
            ->when(isset($where['room_id']) && $where['room_id'] !== '', function ($query) use ($where) {
                $query->where('room_id', intval($where['room_id']));
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', intval($where['is_type']));
            })
            ->when(isset($where['bill_type']) && $where['bill_type'] !== '', function ($query) use ($where) {
                $query->where('bill_type', intval($where['bill_type']));
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', intval($where['contract_id']));
            })
            ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
                if (is_array($where['status'])) {
                    $query->whereIn('status', $where['status']);
                } else {
                    if ($where['status'] == -1) {
                        $query->whereIn('status', [0, 1, 2, 3, 8, 5]); ## 待处理账单
                    } elseif ($where['status'] == -2) {
                        $query->where('invoice_status', 2); ## 发票状态
                    } elseif ($where['status'] > 0) {
                        $query->where('status', (int)$where['status']);
                    }
                }
            })
            ->when(isset($where['month_time']) && $where['month_time'] !== '', function ($query) use ($where) {
                $query->where('month_time', $where['month_time']);
            })
            ->when(isset($where['month']) && $where['month'] !== '', function ($query) use ($where) {
                $query->where('month_time', $where['month']);
            })
            ->when(isset($where['year']) && $where['year'] !== '', function ($query) use ($where) {
                $query->where('month_time', 'between', [$where['year'] . '-01', $where['year'] . '-12']);
            })
            ->when(isset($where['create_date']) && $where['create_date'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['create_date'], 'create_date');
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('number|remark', '%' . trim($where['keywords']) . '%');
            });
    }

    public function deleteByBillId(int $id)
    {
        return $this->getSearch([
            'id' => $id
        ])->delete();
    }
}
